/** SimpleServer.java:
 * 	This server serves only one client connection.
 * 	*/

import java.io.*;
import java.net.*;

public class SimpleServer {

    public static void main(String arg[]){
	ServerSocket		ssk = null;
	 InputStream		reader= null;
	OutputStream		writer = null;
	Socket			sk = null;
	byte			buf[] = new byte[200];
	String			msg = null;
	int			msgCount = 0;

	try {
	    ssk = new ServerSocket(9901);
	    sk = ssk.accept();
	    System.out.printf("A connection is made!\n");

	    reader = sk.getInputStream() ;
	    writer = sk.getOutputStream() ;

	    int	k = 0;

	    while ( true ) {

		k = reader.read(buf); 

	        if ( k > 0 ) {
		    System.out.printf("SERVER: %s\n", new String(buf) );
		}
		msg = "SERVER MESSAGE " + (++ msgCount);
	    	writer.write(msg.getBytes());
		writer.flush();

	    	Thread.sleep( 1000 );
	    }
	}
	catch(Exception e) { e.printStackTrace();  } 

    }

}
